//权限工具类
import 'dart:io';

import 'package:device_info_plus/device_info_plus.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
import 'package:permission_handler/permission_handler.dart';

import 'package:lib_bt/BleManager.dart';
import 'package:lib_util/DialogUtil.dart';

import '../../generated/l10n.dart';

class PermissionUtil {
  static PermissionUtil? _instance;

  static PermissionUtil get instance => _instance ??= PermissionUtil._();
  final permissionMethodChannel = const MethodChannel("permission_method_channel");

  PermissionUtil._();

  //检测蓝牙相关权限
  Future<bool> checkBlePermission(BuildContext context) async {
    bool flag = true;
    if (Platform.isAndroid) {
      var permissionList = [
        Permission.location,
        Permission.bluetoothConnect,
        Permission.bluetoothScan,
        Permission.locationWhenInUse,
        //Permission.microphone,
      ];

      /*DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
      AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;

      if (androidInfo.version.sdkInt <= 32) {
        permissionList.add(Permission.storage);
      } else {
        permissionList.add(Permission.photos);
        permissionList.add(Permission.audio);
      }*/

      Map<Permission, PermissionStatus> statuses = await permissionList.request();

      for (var key in statuses.keys) {
        var value = statuses[key];
        if (value != PermissionStatus.granted) {
          ///弹窗提示申请权限
          DialogUtil.showDialogEz(context, S.of(context).str_common_5, getPermissionTip(context, key),
              S.of(context).str_common_2, S.of(context).str_common_6, () {
            openAppSettings();
            //checkPermission(context);
          });
          flag = false;
          break;
        }
      }
    } else if (Platform.isIOS) {}
    if (flag) {
      //权限全部通过,检测gps和蓝牙开关
      flag = await BleManager.instance.isBleEnable();
      if (!flag) {
        if (Platform.isAndroid) {
          BleManager.instance.openBle();
        } else {
          DialogUtil.showDialogEz(context, S.of(context).str_common_5, S.of(context).str_common_8,
              S.of(context).str_common_2, S.of(context).str_common_6, () {
            BleManager.instance.openGps();
          });
        }
        return flag;
      }
      flag = await BleManager.instance.isGpsEnable();
      if (!flag) {
        ///弹窗提示打开GPS服务
        DialogUtil.showDialogEz(context, S.of(context).str_common_5, S.of(context).str_common_7,
            S.of(context).str_common_2, S.of(context).str_common_6, () {
          BleManager.instance.openGps();
        });
        return flag;
      }
    }
    return flag;
  }

  String getPermissionTip(BuildContext context, Permission permission) {
    String tip = "";
    switch (permission) {
      case Permission.location:
        tip = S.of(context).str_common_10;
        break;
      case Permission.bluetoothConnect:
      case Permission.bluetoothScan:
      case Permission.locationWhenInUse:
        tip = S.of(context).str_common_11;
        break;
      case Permission.photos:
      case Permission.audio:
        tip = S.of(context).str_common_4;
        break;
      default:
        tip = S.of(context).str_common_11;
    }
    return tip;
  }

  ///检测是否有音乐的权限
  Future<bool> checkMusicPermission(BuildContext context) async {
    if (Platform.isAndroid) {
      List<Permission> permissionList = [Permission.microphone];
      DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
      AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;

      if (androidInfo.version.sdkInt <= 32) {
        permissionList.add(Permission.storage);
      } else {
        permissionList.add(Permission.audio);
      }

      Map<Permission, PermissionStatus> statuses = await permissionList.request();

      bool flag = true;
      for (var key in statuses.keys) {
        var value = statuses[key];
        if (value != PermissionStatus.granted) {
          String tip = "";
          switch (key) {
            case Permission.storage:
            case Permission.audio:
            case Permission.photos:
              tip = S.of(context).str_common_4;
              break;
            case Permission.microphone:
              tip = S.of(context).str_common_15;
              break;
          }

          ///弹窗提示申请权限
          DialogUtil.showDialogEz(
              context, S.of(context).str_common_5, tip, S.of(context).str_common_2, S.of(context).str_common_6, () {
            openAppSettings();
            //checkPermission(context);
          });
          flag = false;
          break;
        }
      }
      return flag;
    } else if (Platform.isIOS) {
      final ret = await permissionMethodChannel.invokeMethod("hasMusicPermission");
      print("ret:${ret["isDenied"]}");

      if (ret["isDenied"]) {
        ///弹窗提示打开GPS服务
        DialogUtil.showDialogEz(context, S.of(context).str_common_5, S.of(context).str_common_17,
            S.of(context).str_common_2, S.of(context).str_common_6, () {
          permissionMethodChannel.invokeMethod("openAuthorized");
        });
        return false;
      }
    }
    return true;
  }


  ///检测是否有拍照的权限
  Future<bool> checkCameraPermission(BuildContext context) async {
    if (Platform.isAndroid) {
    } else if (Platform.isIOS) {
      final ret =
      await permissionMethodChannel.invokeMethod("hasCameraPermission");
      print("ret:${ret["isDenied"]}");

      if (ret["isDenied"]) {
        ///弹窗提示打开GPS服务
        DialogUtil.showDialogEz(
            context,
            S.of(context).str_common_5,
            S.of(context).str_common_14,
            S.of(context).str_common_2,
            S.of(context).str_common_6, () {
          permissionMethodChannel.invokeMethod("openAuthorized");
        });
        return false;
      }
    }
    return true;
  }

  ///检测是否有选择照片的权限
  Future<bool> checkPhotoPermission(BuildContext context) async {
    if (Platform.isAndroid) {
    } else if (Platform.isIOS) {
      final ret =
      await permissionMethodChannel.invokeMethod("hasPhotoPermission");
      print("ret:${ret["isDenied"]}");

      if (ret["isDenied"]) {
        ///弹窗提示打开GPS服务
        DialogUtil.showDialogEz(
            context,
            S.of(context).str_common_5,
            S.of(context).str_common_16,
            S.of(context).str_common_2,
            S.of(context).str_common_6, () {
          permissionMethodChannel.invokeMethod("openAuthorized");
        });
        return false;
      }
    }
    return true;
  }
}
